package com.tianruan.dc.modules.userinfo.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tianruan.dc.common.utils.response.VResponse;
import com.tianruan.dc.modules.sys.user.entity.dto.UserDTO;
import com.tianruan.dc.modules.sys.user.utils.UserUtils;
import com.tianruan.dc.modules.userinfo.entity.AdminPageDTO;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import com.tianruan.dc.modules.userinfo.entity.Admin;
import com.tianruan.dc.modules.userinfo.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.Objects;

@RestController
@RequestMapping("/api/admin")
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;

    /*
    查看管理员
     */
    @GetMapping("/{id}")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin')")
    public VResponse<AdminPageDTO> getById(@PathVariable String id) {
        log.info("账号：{}", id);
        AdminPageDTO adminPageDTO = adminService.getById(id);
        if(ObjectUtil.isEmpty(adminPageDTO)){
            return VResponse.error(1000,"该管理员未注册！");
        }
        return VResponse.success(adminPageDTO);
    }

    /*
    修改管理员(对管理员信息进行修改)
     */
    @PutMapping("/update")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin')")
    public VResponse<Object> update(@RequestBody Admin admin) {
        log.info("修改管理员信息：{}", admin);
        /*
        检查该管理员是否被删除
         */
        boolean check = adminService.check(admin);
        if(check){
            adminService.update(admin);
            return VResponse.success("修改成功！");
        }
       else {
           return VResponse.error(1000,"该管理员已被删除，您无法修改！");
        }
    }

    /*
    删除管理员（即将delFlag改为1）
     */
    @DeleteMapping("/delete")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin')")
    @Transactional
    public VResponse<Object> delete(@RequestBody Admin id) {
        UserDTO userDTO = UserUtils.getUser();
        if(!Objects.equals(userDTO.getId(), "1")) {
            return VResponse.error(1000, "您不是超管，不可删除管理员！");
        } else {
            adminService.delete(id);
            return VResponse.success("成功删除");
        }
    }

    /*
    管理员姓名，联系方式，工作单位模糊搜索·1
     */
    @GetMapping("/page")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin')")
    public VResponse<PageBean<AdminPageDTO>> page(
            @RequestParam(defaultValue = "1")  Integer page,
            @RequestParam(defaultValue = "10")  Integer pageSize,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "userName",required = false) String userName,
            @RequestParam(value = "phone",required = false) String phone,
            @RequestParam(value = "position",required = false) String position,
            @RequestParam(value = "deptId",required = false) String deptId,
            @RequestParam(value = "roleId",required = false) String roleId,
            @RequestParam(value = "status",required = false) String status
                                                 ) {
            PageBean<AdminPageDTO> list = adminService.pageAdmin(page,pageSize,name,userName,phone,position,deptId,roleId,status);
            return VResponse.success(list);
    }
    /*
    重置管理员的密码
     */
    @PutMapping("/update/{id}")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin')")
    public VResponse<Object> updatePassword(@PathVariable String id){
        /*
        重置密码为123456
         */
//        UserDTO currentUser = UserUtils.getUser();
        boolean admin=adminService.findAdmin(id);
        if (admin){
            log.info("重置密码{}",id);
            adminService.updatePassword(id);
            return VResponse.success("重置密码成功！");
        }
        else {
            return VResponse.error(1000,"该管理员不存在，您无法重置！");
        }

    }
}
